
  var table = layui.table;
  var form = layui.form;

  $("#dictTypeStatusSelect").initSelectDict();
  $("#dictValueStatusSelect").initSelectDict();
	
  initTable({
	 elem: '#dictType-t'
    ,url: ctx+'/sys/dictType/getPage'
    ,cols: [[
       {type:'radio'}
      ,{field:'typeId', title: 'ID', hide:true }
      ,{field:'typeName', minWidth:150, title: '类型名', sort: true}
      ,{field:'typeCode', minWidth:150, title: '类型编码', sort: true}
      ,{field:'sort', minWidth:50, title: '排序', sort: true}
      ,{field:'remark', minWidth:100, title: '备注'}
      ,{field:'status', minWidth:50, title: '状态', sort: true
    	  ,templet: function(d){
    		  return transDict("status",d.status);
    	   }
      }
      ,{
			title: '操作',
			align: 'center',
			toolbar: '#dictType-t-tool',
			fixed: 'right',
			minWidth: 150
		}
    ]]
  })
  
  
  
  //监听字典类型工具条
  table.on('tool(dictType-t)', function(obj){
    var data = obj.data;
    var id = data.typeId;
    var typeCode = data.typeCode;
    if(obj.event === 'del'){
      layer.confirm('确定删除吗？', function(index){
    	  ajaxCommit({
    		  url : ctx+"/sys/dictType/del?typeCode="+typeCode
    		  ,successfunc: function(){
  					dictTypeTableReload();
  				} 
    	  })
      });
    } else if(obj.event === 'edit'){
		var url = ctx+"/sys/dictType/editView?id="+id;
		$.get(url, {}, function(res){
			if(!isJSON(res)){
			  var index = layer.open({
					type: 1,
					title: '编辑',
					skin: 'layui-layer-rim', 
					area: ['30%', '55%'],
					offset: ['5%', '20%'],
			        content: res,
					btn: [],
					end: function(){
						dictTypeTableReload();
					}
			  		,maxmin: true
			  });
			  layer.full(index);
			}
		  });
    }
  });
  
//监听行单击事件
  table.on('row(dictType-t)', function(obj){
    var typeCode = obj.data.typeCode;
    var where = {"typeCode":typeCode};
    initTable({
    	 where: where
		,elem: '#dictValue-t'
	    ,url: ctx+'/sys/dictValue/getPage'
	    ,cols: [[
	       {type:'radio'}
	      ,{field:'valueId',  title: 'ID', hide: true}
	      ,{field:'valueName', minWidth:150, title: '字典名', sort: true}
	      ,{field:'valueCode', minWidth:150, title: '字典编码', sort: true}
	      ,{field:'sort', minWidth:50, title: '排序', sort: true}
	      ,{field:'remark', minWidth:100, title: '备注'}
	      ,{field:'status', minWidth:50, title: '状态', sort: true 
	    	  ,templet: function(d){
	    		  return transDict("status",d.status);
	    	   }
	      }
	      ,{
				title: '操作',
				align: 'center',
				toolbar: '#dictValue-t-tool',
				fixed: 'right',
				minWidth: 150,
			}
	    ]]
	  })
	  
	//标注选中样式
	obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
    //选中radio样式
    obj.tr.find('i[class="layui-anim layui-icon"]').trigger("click");
    
  });
  
  //监听字典值工具条
  table.on('tool(dictValue-t)', function(obj){
    var id = obj.data.valueId;
    if(obj.event === 'del'){
      layer.confirm('确定删除吗？', function(index){
    	  ajaxCommit({
    		  url : ctx+"/sys/dictValue/del?id="+id
    		  ,successfunc: function(){
  					dictValueTableReload();
  				}  
    	  })
      });
    } else if(obj.event === 'edit'){
		var url = ctx+"/sys/dictValue/editView?id="+id;
		$.get(url, {}, function(res){
			if(!isJSON(res)){
			  var index = layer.open({
					type: 1,
					title: '编辑',
					skin: 'layui-layer-rim', 
					area: ['30%', '55%'],
					offset: ['5%', '20%'],
			        content: res,
					btn: [],
					end: function(){
						dictValueTableReload();
					}
					,maxmin: true
			  });
			  layer.full(index);
			}
		});
    }
  });
  
  //字典类型table重载
  function dictTypeTableReload(){
  	var where = $("#dictTypeSearchForm").serializeObject();
  	 layui.table.reload("dictType-t", {
  		 where: where
  		,page: {
  		      curr: 1 
  		}
  	}); 
  }

  //table工具栏搜索查询
  $("#dictTypeSearch").click(function(){
	  dictTypeTableReload();
  })
  
  
  //字典值table重载
  function dictValueTableReload(){
  	var where = $("#dictValueSearchForm").serializeObject();
    var typeData = table.checkStatus('dictType-t').data[0];
    where.typeCode = typeData.typeCode;
  	 layui.table.reload("dictValue-t", {
  		where: where
  		,page: {
  		      curr: 1 
  		}
  	}); 
  }

  $("#dictValueSearch").click(function(){
	  dictValueTableReload();
  })

  $("#dictValueAdd").click(function(){
	  var typeData = table.checkStatus('dictType-t').data[0];
	  if(!isNotNull(typeData)){
	    	 layer.alert("请先选择字典类型", {
	   	      title: '提示：'
	   	    });
	     return false;
	  }
	  var typeCode = typeData.typeCode;
		var title = $(this).text();
		var url = ctx+"/sys/dictValue/addView?typeCode="+typeCode;
		$.get(url, {}, function(res){
			if(!isJSON(res)){
			  var index = layer.open({
					type: 1,
					title: title,
					skin: 'layui-layer-rim', 
					area: ['30%', '55%'],
					offset: ['5%', '20%'],
			        content: res,
					btn: [],
					end: function(){
						dictValueTableReload();
					}
			  		,maxmin: true
			  });
			  layer.full(index);
			}
		});
  })

  	//字典类型添加
	$("#dictTypeAdd").click(function(){
		var title = $(this).text();
		var url = ctx+"/sys/dictType/addView";
		$.get(url, {}, function(res){
			if(!isJSON(res)){
			  var index = layer.open({
					type: 1,
					title: title,
					skin: 'layui-layer-rim', 
					area: ['30%', '55%'], 
					offset: ['5%', '20%'],
			        content: res,
					btn: [],
					end: function(){
						dictTypeTableReload();
					}
			        ,maxmin: true
			  });
			  layer.full(index);
			}
		});
	}) 
	
	//初始化字典缓存
	$("#initDictCache").click(function(){
  	  ajaxCommit({
		  url : ctx+"/sys/dictType/initDictCache"
	  })
	})
